package com.sun.local.generate;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成全量数据
 */
public class GenerateAllData {

    public static final String SSQ_ALL_FILE_PATH = System.getProperty("user.dir") + File.separator + "data" +
            File.separator + "all" + File.separator + "ssq_all.text";

    public static void main(String[] args) {
        // 生成所有红球组合
        List<List<String>> redCombinations = getRedCombinations();

        // 写出数据
        waiteAcData(redCombinations);
    }

    /**
     * 生成全量红球组合数据<br>
     * 规则：从1~33中随机选择6个不重复数字
     *
     * @return 全量红球组合数据
     */
    public static List<List<String>> getRedCombinations() {
        List<List<String>> combinations = new ArrayList<>();
        for (int i = 1; i < 29; i++) {
            for (int j = 2; j < 30; j++) {
                if (i >= j) {
                    continue;
                }
                for (int k = 3; k < 31; k++) {
                    if (j >= k) {
                        continue;
                    }
                    for (int l = 4; l < 32; l++) {
                        if (k >= l) {
                            continue;
                        }
                        for (int m = 5; m < 33; m++) {
                            if (l >= m) {
                                continue;
                            }
                            for (int n = 6; n < 34; n++) {
                                if (m >= n) {
                                    continue;
                                }
                                List<String> combination = new ArrayList<>();
                                combination.add(fillZero(i));
                                combination.add(fillZero(j));
                                combination.add(fillZero(k));
                                combination.add(fillZero(l));
                                combination.add(fillZero(m));
                                combination.add(fillZero(n));
                                combinations.add(combination);
                            }
                        }
                    }
                }
            }
        }
        return combinations;
    }

    /**
     * 小于10的数字，前面加0。
     *
     * @param num 数字
     * @return 数字字符
     */
    private static String fillZero(int num) {
        String numStr = "";
        if(num < 10) {
            numStr = "0" + num;
        } else {
            numStr = String.valueOf(num);
        }
        return numStr;
    }

    /**
     * 写出数据
     *
     * @param combinations 全部红球组合
     */
    private static void waiteAcData(List<List<String>> combinations) {
        long startTime = System.currentTimeMillis();
        List<String> outputList = new ArrayList<>();
        for (List<String> combination : combinations) {
            outputList.add(StringUtils.join(combination, ","));
        }
        // 清空文件内容
        FileUtil.writeUtf8String("", SSQ_ALL_FILE_PATH);
        // 写出数据
        FileUtil.appendUtf8Lines(outputList, SSQ_ALL_FILE_PATH);
        long endTime = System.currentTimeMillis();
        System.out.println("写出数据完成！数量：" + outputList.size() + " 消耗时间：" + (endTime - startTime) + "ms");
    }

}
